
package com.jf.cloud.group.feign;

import cn.hutool.core.collection.CollUtil;
import com.jf.cloud.api.group.feign.GroupFeignClient;
import com.jf.cloud.common.product.vo.GroupActivitySpuVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.group.constant.GroupActivityStatusEnum;
import com.jf.cloud.group.service.GroupActivityService;
import com.jf.cloud.group.vo.GroupActivityVO;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Hidden;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author zz
 * @date 2020/12/8
 */
@RestController
@Hidden
public class GroupFeignController implements GroupFeignClient {

    @Autowired
    private GroupActivityService groupActivityService;

    @Autowired
    private MapperFacade mapperFacade;

    @Override
    public ServerResponseEntity<Date> getActivityStartTime(Long activityId) {
        GroupActivityVO groupActivity = groupActivityService.getByGroupActivityId(activityId);
        if (!Objects.equals(groupActivity.getStatus(), GroupActivityStatusEnum.ENABLE.value())) {
            return ServerResponseEntity.success();
        }
        Date date = new Date();
        Date startTime = null;
        // 活动结束时间小于等于当前时间，
        if (groupActivity.getEndTime().getTime() <= System.currentTimeMillis()) {
            return ServerResponseEntity.success(startTime);
        }
        // 预热活动的开始时间设为当前
        if (Objects.equals(groupActivity.getIsPreheat(), 1)) {
            startTime = new Date();
        }
        // 非预热活动的开始时间为活动开始时间
        else if (date.getTime() < groupActivity.getEndTime().getTime()) {
            startTime = groupActivity.getStartTime();
        }
        return ServerResponseEntity.success(startTime);
    }

    @Override
    public ServerResponseEntity<com.jf.cloud.api.group.vo.GroupActivityVO> getActivityInfo(Long activityId) {
        GroupActivityVO groupActivity = groupActivityService.getByGroupActivityId(activityId);
        com.jf.cloud.api.group.vo.GroupActivityVO groupActivityVO = mapperFacade.map(groupActivity, com.jf.cloud.api.group.vo.GroupActivityVO.class);
        return ServerResponseEntity.success(groupActivityVO);
    }

    @Override
    public ServerResponseEntity<List<GroupActivitySpuVO>> groupSpuListBySpuIds(List<Long> spuIds) {
        if (CollUtil.isEmpty(spuIds)) {
            return ServerResponseEntity.success(new ArrayList<>());
        }
        List<GroupActivitySpuVO> groupActivitySpuList = groupActivityService.groupSpuListBySpuIds(spuIds);
        return ServerResponseEntity.success(groupActivitySpuList);
    }

    @Override
    public ServerResponseEntity<Void> offlineGroupBySpuIds(List<Long> spuIds) {
        groupActivityService.offlineGroupBySpuIds(spuIds);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> updateGroupActivityPrice(List<Long> skuIds, Long spuId) {
        groupActivityService.updateGroupActivityPrice(skuIds, spuId);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> updateGroupActivityStatus(Long spuId) {
        groupActivityService.updateGroupActivityStatus(spuId);
        return ServerResponseEntity.success();
    }
}
